package com.zangchat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.common.core.domain.AjaxResult;
import com.common.utils.StringUtils;
import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayOrder;
import com.zangchat.config.AliPayDataConfig;
import com.zangchat.domain.ChatAccount;
import com.zangchat.domain.ChatLogCash;
import com.zangchat.domain.ChatLogThirdpay;
import com.zangchat.domain.ChatUsers;
import com.zangchat.service.*;
import com.zangchat.utils.DateUtil;
import com.zangchat.utils.HttpUtil;
import com.zangchat.utils.Md5Utils;
import com.zangchat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by GaoTing on 2019/4/29
 * Explain:自定义 服务层实现 .处理个人中心业务
 */
@Service
public class PersonalServiceImpl implements IPersonalService {

    @Autowired
    private IChatAccountService chatAccountService;
    @Autowired
    private IChatUsersService chatUsersService;

    @Autowired
    private IChatUsersRechargeService chatUsersRechargeService;
    @Autowired
    private IChatLogThirdpayService chatLogThirdpayService;

    /**
     * 个人中心 账户余额及关注数量等
     *
     * @param userId
     * @return
     */
    @Override
    public AjaxResult inTopersonal(Long userId) {
        AjaxResult ajaxResultUser = chatUsersService.checkedUser(userId);
        if (Integer.parseInt(ajaxResultUser.get("code")+"")!=1){
            return ajaxResultUser;
        }
        Map<String,Object> resultMap=new HashMap<>();
        ChatAccount chatAccount=new ChatAccount();
        chatAccount.setUserId(userId);
        QueryWrapper<ChatAccount> queryWrapper1=new QueryWrapper<>(chatAccount);
        ChatAccount chatAccount1 = chatAccountService.getBaseMapper().selectOne(queryWrapper1);
        BigDecimal ordinaryCash = chatAccount1.getOrdinaryCash();

        ChatUsers chatUsers = chatUsersService.getBaseMapper().selectById(userId);
        Integer concernNum = chatUsers.getConcernNum();
        resultMap.put("ordinaryCash",ordinaryCash);//账户余额
        resultMap.put("concernNum",concernNum); //关注人数
        resultMap.put("userName",chatUsers.getUserName());//昵称
        resultMap.put("userPhone",chatUsers.getUserPhone());//手机号
        resultMap.put("sex",chatUsers.getSex());//性别
        resultMap.put("headImg",chatUsers.getHeadImg().equals(null)?"":chatUsers.getHeadImg());//头像
        resultMap.put("ID",chatUsers.getId()); //ID
        Long days = DateUtil.dateDiff(chatUsers.getCreateTime(), new Date());
        resultMap.put("registerDays",days); //注册天数
        resultMap.put("yunCoin",chatAccount1.getYunCoin()); //云币
        resultMap.put("yunPaper",chatAccount1.getYunPaper()); //云票
        resultMap.put("ZLB",chatAccount1.getZlCoin());
        return AjaxResult.success(resultMap);
    }
    /**
     * 用户充值 生成订单并吊起App
     * @param userId 用户id
     * @param cash 充值金额
     * @param payType 支付方式 '充值方式1:线下现金支付2.支付宝3.微信4银联快捷5.第三方支付',
     * @return
     */
    @Override
    public AjaxResult recharge(Long userId, BigDecimal cash,Integer payType) {
        AjaxResult ajaxResultUser = chatUsersService.checkedUser(userId);
        if (Integer.parseInt(ajaxResultUser.get("code")+"")!=200){
            return ajaxResultUser;
        }
        if (StringUtils.isNull(cash)){
            return AjaxResult.error("请输入充值金额");
        }
        if (BigDecimal.ZERO.compareTo(cash)==1){
            return AjaxResult.error("输入金额有误，请重新输入");
        }
        //生成订单号
        String orderNo = StringUtils.getOrderIdByTime("c"+userId+"u");
        PayService payService =null;
        if (payType==2){
             payService = chatUsersRechargeService.initAliPayService();
        }
        if (payType==3){
            payService = chatUsersRechargeService.initWxPayService();
        }
        PayOrder payOrder = chatUsersRechargeService.initPayOrder(orderNo, cash, payType);

//        //获取APP支付所需的信息组，直接给app端就可使用
        Map appOrderInfo = payService.orderInfo(payOrder);
        //插入第三方支付记录
        ChatLogThirdpay chatLogThirdpay=new ChatLogThirdpay();
        chatLogThirdpay.setOutTradeNo(orderNo);
        chatLogThirdpay.setUserId(userId);
        chatLogThirdpay.setOrderNo(orderNo);
        chatLogThirdpay.setBusinessType("RECHARGE");
        chatLogThirdpay.setPayType(payType);
        chatLogThirdpay.setIno(AliPayDataConfig.SELLER);
        chatLogThirdpay.setRequest(appOrderInfo.toString());
        chatLogThirdpay.setPayStatus(0);
        chatLogThirdpay.setHandleStatus(0);
        int i = chatLogThirdpayService.getBaseMapper().insert(chatLogThirdpay);
        if (i<1){
            return AjaxResult.error("支付初始化失败");
        }
        return AjaxResult.success(appOrderInfo);
    }
}
